package com.sunyur.tailor.channel.service.inner.inventory.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;
import com.sunyur.tailor.channel.common.inner.util.SunyurClientUtil;
import com.sunyur.tailor.channel.common.inner.util.SunyurResultUtil;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.inventory.model.InventoryBookModel;
import com.sunyur.tailor.channel.service.inner.inventory.param.InventoryParam;
import com.sunyur.tailor.channel.service.inner.inventory.param.InventoryReleaseOrReduceParam;
import com.sunyur.tailor.channel.service.inner.inventory.param.InventorySelectParam;
import com.sunyur.tailor.channel.service.inner.inventory.service.SunyurInventoryService;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @ClassName InventoryServiceImpl
 * @Description 库存台账相关接口
 * @Author xuhui
 * @Date 2021/6/21 17:15
 * @Version 1.0
 */
@Service("sunyurInventoryService")
public class SunyurInventoryServiceImpl implements SunyurInventoryService {

    private static final Logger LOGGER_INFO = LoggerFactory.getLogger(SunyurInventoryServiceImpl.class);
    private static final Logger LOGGER_ERROR = LoggerFactory.getLogger(SunyurInventoryServiceImpl.class);

    /**
     * 请求商越Client
     */
    @Autowired
    private SunyurClientUtil sunyurClient;

    /**
     * 批量创建库存台账接口 链接
     */
    @Value("${sunyur.api.batchInsertInventoryBook}")
    private String batchInsertInventoryBook;

    /**
     * 批量查询库存台账接口 链接
     */
    @Value("${sunyur.api.listInventoryBookByParam}")
    private String listInventoryBookByParam;

    /**
     * 批量增加库存接口 链接
     */
    @Value("${sunyur.api.batchReleaseInventoryBook}")
    private String batchReleaseInventoryBook;

    /**
     * 批量减少库存接口 链接
     */
    @Value("${sunyur.api.batchReduceInventoryBook}")
    private String batchReduceInventoryBook;

    @Override
    public ChannelResult batchInsert(List<InventoryParam> inventoryParamList) throws Exception {
        if (CollectionUtils.isEmpty(inventoryParamList)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), "InventoryServiceImpl batchInsert param is null");
        }
        // 调用主线接口
        SunyurResult sunyurResult = sunyurClient.execPost(batchInsertInventoryBook, JSON.toJSONString(inventoryParamList), null);
        // 主线报错
        if (!SunyurResultUtil.isSuccess(sunyurResult)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
        }
        return ChannelResultUtil.success(sunyurResult.getContent());
    }

    @Override
    public ChannelResult<List<InventoryBookModel>> listInventoryBookByParam(List<InventorySelectParam> inventorySelectParamList) throws Exception {
        if (CollectionUtils.isEmpty(inventorySelectParamList)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), "InventoryServiceImpl inventorySelectParamList param is null");
        }
        // 调用主线接口
        SunyurResult<List<InventoryBookModel>> sunyurResult = sunyurClient.execPost(listInventoryBookByParam, JSON.toJSONString(inventorySelectParamList), new TypeReference<SunyurResult<List<InventoryBookModel>>>() {
        });
        // 主线报错
        if (!SunyurResultUtil.isSuccess(sunyurResult)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
        }
        return ChannelResultUtil.success(sunyurResult.getContent());
    }

    @Override
    public ChannelResult batchRelease(InventoryReleaseOrReduceParam inventoryReleaseOrReduceParam) throws Exception {
        if (ObjectUtils.isEmpty(inventoryReleaseOrReduceParam)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), "InventoryServiceImpl batchRelease param is null");
        }
        // 调用主线接口
        SunyurResult sunyurResult = sunyurClient.execPost(batchReleaseInventoryBook, JSON.toJSONString(inventoryReleaseOrReduceParam), null);
        // 主线报错
        if (!SunyurResultUtil.isSuccess(sunyurResult)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
        }
        return ChannelResultUtil.success(sunyurResult.getContent());
    }

    @Override
    public ChannelResult batchReduce(InventoryReleaseOrReduceParam inventoryReleaseOrReduceParam) throws Exception {
        if (ObjectUtils.isEmpty(inventoryReleaseOrReduceParam)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), "InventoryServiceImpl batchRelease param is null");
        }
        // 调用主线接口
        SunyurResult sunyurResult = sunyurClient.execPost(batchReduceInventoryBook, JSON.toJSONString(inventoryReleaseOrReduceParam), null);
        // 主线报错
        if (!SunyurResultUtil.isSuccess(sunyurResult)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
        }
        return ChannelResultUtil.success(sunyurResult.getContent());
    }

}
